<?php

App::import('Vendor', 'facebook/facebook');

class AppController extends Controller {
  var $view = 'Theme';
  var $theme = 'bp01';
  
  var $helpers = array('Browniepoints', 'Html', 'Form', 'Javascript');
  var $components = array('Browniepoints', 'Session', 'LocalAuth',
    'OpenidAuth' => array('use_database' => true), 'TwitterAuth');
  public $uses = array('Point', 'User', 'LocalAccount', 'OpenidAccount', 'TwitterAccount');

  // cf. http://cutfromthenorth.com/integrating-facebook-connect-with-cakephps-auth-component/
  var $facebook;
  var $__fbApiKey = 'c034e90257ff39ff0e1ff3d7236c98d8';
  var $__fbSecret = 'c7d013c91c49b5f8f095634f7e908d83';
  
  var $op = array('plus', 'minus', 'mal');

  function __construct() {
    parent::__construct();

    // Prevent the 'Undefined index: facebook_config' notice from being thrown.   $GLOBALS['facebook_config']['debug'] = NULL;

    // Create a Facebook client API object.
    //$this->facebook = new Facebook($this->__fbApiKey, $this->__fbSecret);
  }

  function setError($msg, $url = NULL) {
    $this->Session->setFlash(nl2br($msg), 'default', array(), 'auth');
    if ($url) {
      header('Location: ' . Router::url($url)); exit();
      //$this->redirect($url);
    }
  }

  function setSuccess($msg, $url = NULL) {
    $this->Session->setFlash(nl2br($msg));
    if ($url) {
      header('Location: ' . Router::url($url)); exit();
      //$this->redirect($url);
    }
  }

  private function __checkFBStatus(){
    // check to see if a user is not logged in, but a facebook user_id is set
    if (!$this->Auth->user() && $this->facebook->get_loggedin_user()) {

      // see if this facebook id is in the User database; if not, create the user using their fbid hashed as their password
      $fbid = $this->Account->field('remote_id');

      // create new user
      if (!$fbid) {
        $account['fbid'] = $this->facebook->get_loggedin_user();

        $this->Account->create();
        $this->Account->save($account);
      }

      // change the Auth fields
      $this->Auth->fields = array('username' => 'fbid', 'password' => 'password');

      // log in the user with facebook credentials
      $this->Auth->login($user_record);
    }
  }

  function beforeFilter() {
    parent::beforeFilter();
    $auth_component = $this->Session->read('auth_component');
    if (!$auth_component) {
      $auth_component = 'LocalAuth';
    }
    $this->Auth = $this->{$auth_component};
    // error_log("AUTH set to $auth_component " . json_encode($auth_component));

    $this->account = $this->Auth->user();
    if ($this->account) {
      $user_id = $this->User->user_id($this->account);
      $this->account['type'] = preg_replace('/Account$/', '', key($this->account));

      if ($user = $this->User->find('first', array(
        'conditions' => array('User.id' => $user_id),
        'recursive' => -1))) {
        $this->account += $user;
      }
    }

    $this->set('account', $this->account);
  }

  function beforeRender() {
    $this->Browniepoints->css('reset');
    $this->Browniepoints->css('main');
    $this->Browniepoints->css('main-msie', array('conditional' => 'lte IE 6'));
    $this->Browniepoints->css('style');
    $this->Browniepoints->css('browniepoints');
    $this->Browniepoints->css('print', array('media' => 'print'));
    
    $this->Browniepoints->js('jquery-1.3.2.min');
    $this->Browniepoints->js('inline', 'global_js');
    
    $this->Browniepoints->meta('robots', "all,follow");
    $this->Browniepoints->meta('author', array('lang' => "en", 'content' => "All: Olav Schettler [olav.net]; e-mail: firstname at browniepoints.de"));
    
    if ($this->theme == 'bp01') {
      $this->Browniepoints->meta('copyright', array('lang' => "en", 'content' => "Olav Schettler"));
    }
    else {
      $this->Browniepoints->meta('copyright', array('lang' => "en", 'content' => "Webdesign: Nuvio [www.nuvio.cz]; e-mail: ahoj@nuvio.cz"));
    }

    $this->Browniepoints->meta('description', "Treat your peers with brownie points");
    $this->Browniepoints->meta('keywords', "brownie points, gratification");
  }

  function setCaptcha() {
    // prep form
    $a = rand(1, 10);
    $b = rand(1, 10);

    // make sure minus yields a positive result
    if ($a > $b) {
      $calc['a'] = $a;
      $calc['b'] = $b;
    }
    else {
      $calc['a'] = $b;
      $calc['b'] = $a;
    }
    $calc['n'] = rand(0, 2);
    $this->set('calc', $calc + array('op' => $this->op[$calc['n']]));
    $this->Session->write('calc', $calc);
  }

  function checkCaptcha() {      
    $calc = $this->Session->read('calc');
    $chk = intval($this->data['chk']);
    //echo  'Aufgabe: ' . $calc['a'] . $this->op[$calc['n']] . $calc['b'] . ' = ' . $chk . "\n";

    $correct = FALSE;
    switch ($calc['n']) {
      case 0: 
        if ($chk == $calc['a'] + $calc['b']) {
          $correct = TRUE;
        }
        break;
          
      case 1:
        if ($chk == $calc['a'] - $calc['b']) {
          $correct = TRUE;
        }
        break;
        
      case 2:
        if ($chk == $calc['a'] * $calc['b']) {
          $correct = TRUE;
        }
        break;
    }
    return $correct;
  }
}
